﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AutoLoL.Logic.Parsers
{
    public class FontConfigParser
    {
        private Dictionary<string, string> _data = null;

        private static FontConfigParser _instance;
        public static FontConfigParser Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FontConfigParser();
                }

                return _instance;
            }
        }

        private FontConfigParser()
        {
            string projectsPath = Path.Combine(UserSettings.Instance.LolDirectory, "rads", "projects");

            string[] clientPaths = new string[] { "lol_game_client_en_us", "lol_game_client_en_gb", "lol_game_client_*_*" };
            
            string fontConfigPath = null;
            
            foreach (string clientPath in clientPaths)
            {
                string[] clientVersionsPath = Directory.GetDirectories(projectsPath, clientPath);
                if (clientVersionsPath.Length == 0) continue;

                foreach (string clientVersionPath in clientVersionsPath)
                {
                    string[] fontConfigFiles = Directory.GetFiles(clientVersionPath, "fontconfig_*_*.txt", SearchOption.AllDirectories);
                    if(fontConfigFiles.Length == 0)continue;

                    fontConfigPath = fontConfigFiles[fontConfigFiles.Length - 1];

                    if (!string.IsNullOrEmpty(fontConfigPath)) break;
                }
            }

            if (fontConfigPath == null) throw new NullReferenceException("Unable to locate a fontconfig file...");
            StreamReader reader = File.OpenText(fontConfigPath);

            _data = new Dictionary<string, string>();

            using (reader)
            {
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    // TODO - Use regex instead
                    string prefix = "tr \"";
                    string suffix = "\"";
                    string[] split = new string[] { "\" = \"" };
                    if (line.StartsWith(prefix) && line.EndsWith(suffix))
                    {
                        line = line.Substring(prefix.Length, line.Length - (prefix.Length + suffix.Length));

                        string[] keyValue = line.Split(split, StringSplitOptions.RemoveEmptyEntries);
                        if (keyValue.Length == 2)
                        {
                            _data.Add(keyValue[0], keyValue[1]);
                        }
                    }
                }
            }
        }

        public bool ContainsKey(string key)
        {
            return _data.ContainsKey(key);
        }

        public string GetValue(string key)
        {
            if (ContainsKey(key))
            {
                return _data[key];
            }

            return null;
        }
    }
}
